package com.qilu.pe.ui.activity;

import android.os.Bundle;

import com.androidkun.xtablayout.XTabLayout;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.dao.bean.Patient;
import com.qilu.pe.support.util.ActiviceCollector;
import com.qilu.pe.ui.fragment.PlaceOrder1Fragment;
import com.qilu.pe.ui.fragment.PlaceOrder2Fragment;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

// 预约下单
public class PlaceOrderActivity extends Base2Activity {
    private XTabLayout tab_2;
    private ViewPager vp_tab;

    private VpTabAdapter vpTabAdapter;// tab
    private static final String[] HOME_MODELS = {"信息录入", "预约列表"};

    private Patient patient;// 患者页面-立即预约-传过来的患者对象

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_place_order;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActiviceCollector.addActivity(this);
        tab_2 = findViewById(R.id.tab_2);
        vp_tab = findViewById(R.id.vp_tab);

        setHeadTitle("预约下单");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            patient = (Patient) bundle.getSerializable("patient");
        }

        for (int i = 0; i < 2; i++) {
            tab_2.addTab(tab_2.newTab().setText(HOME_MODELS[i]), i);
        }
        vpTabAdapter = new VpTabAdapter(getSupportFragmentManager());
        vp_tab.setAdapter(vpTabAdapter);
        tab_2.setupWithViewPager(vp_tab);
    }

    // 预约下单 tab adapter
    private class VpTabAdapter extends FragmentPagerAdapter {

        public VpTabAdapter(@NonNull FragmentManager fm) {
            super(fm);
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            if (position == 1) {
                return PlaceOrder2Fragment.newInstance("2");
            }

            return PlaceOrder1Fragment.newInstance("1",patient);
        }

        @Override
        public int getCount() {
            return 2;
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {
            return HOME_MODELS[position];
        }
    }
}